<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/25 0025
 * Time: 9:50
 */

namespace app\manager\controller;

use classes\Node;
use \think\Db;
use member\captain;


class User extends Base
{
    function _initialize()
    {
        Node::generate();
        parent::_initialize();
        $this->assign("controller",config("controller"));
        $this->assign("action",config("action"));
    }

    //管理员用户列表
    public function index()
    {
        $list = Db::table("member")->order("id desc")->paginate(3);
        $page = $list->render();
        $this->assign('list',$list);
        $this->assign("page",$page);
        return $this->fetch();
    }

    //添加管理员页面
    public function adduser()
    {
        return $this->fetch("adduser");
    }

    //执行添加操作
    public function addusering()
    {
        $username = input('post.username');
        $password = input('post.password');
        $phone = input('post.phone');
        $wechat = input('post.wechat');
        $data = array(
            "username" => $username,
            "password" => captain::password_encrypt($password),
            "mobile" => $phone,
            "wechat" => $wechat,
        );
        $data['ip_register'] = "1212";
        $data['time_register'] = "0000-00-00 00:00:00";
        $result = Db::table("member")->insert($data);
        if($result){
            $this->success('新增成功', 'manager/user/index');
        }else{
            $this->error('新增失败');
        }
    }

    //编辑管理员信息页面
    public function edituser()
    {
        $id = input('get.id');
        if(empty($id)){
            $this->error('参数错误！');
        }
        $userinfo = Db::table("member")->where("id",$id)->find();
        $this->assign("userinfo",$userinfo);
        return $this->fetch("edituser");
    }

    //执行编辑
    public function editusering()
    {
        $id = input('post.id');
        if(empty($id)){
            $this->error('参数错误！');
        }
        $username = input('post.username');
        $phone = input('post.phone');
        $wechat = input('post.wechat');
        $data = array(
            "username" => $username,
            "mobile" => $phone,
            "wechat" => $wechat,
        );
       $result =  Db::table("member")->where("id",$id)->update($data);
        if($result > 0){
            $this->success('修改成功', 'manager/user/index');
        }else{
            $this->error('数据无修改');
        }
    }

    //删除信息
    public function deluser()
    {
        $id = input('get.id');
        if(empty($id)){
            $this->error('参数错误！');
        }
        $result = Db::table("member")->where("id",$id)->delete();
        if($result){
            $this->success('删除成功', 'manager/user/index');
        }else{
            $this->error('删除失败');
        }
    }

    //添加用户角色的编辑页面
    public function addrole(){
        $id = input('get.id');
        if(empty($id)){
            $this->error('参数错误！');
        }
        $userinfo = Db::table("member")->where("id",$id)->find();
        //$admin = Db::table("users_role")->where("mid", $id)->field('id_role')->select();
        $checked = Db::table("users_role")->where("mid",$id)->field('id_role')->select();
        $count = count($checked);
        if($count==0){
            $checked = 0;
            $this->assign("checked",$checked);
        }
        if($count==1){
            $this->assign("checked",$checked);
        }
        if($count==2){
                $this->assign("checked",$checked);
        }
        $this->assign("count",$count);
        $this->assign("userinfo",$userinfo);
        return $this->fetch("addrole");
    }
    //执行添加用户角色操作
    public function addroleing(){
        $id = input('post.id');
        if(empty($id)){
            $this->error('参数错误!');
        }
        $mid = input("post.id");
        $id_role = input("post.admin/a",[]);
        $checked = Db::table("users_role")->where("mid",$mid)->field("id_role")->select();
        $check = array();
        foreach($checked as $value){
            foreach($value as $key=>$val){
                $check = $val;
            }
        }
        var_dump($check);
    }

}